package com.isoftstone.userinfo.controller;

import com.isoftstone.platform.controller.BaseController;
import com.isoftstone.platform.entity.PadingRstType;
import com.isoftstone.platform.entity.PagingBean;
import com.isoftstone.platform.model.entity.PtUserEntity;
import com.isoftstone.userinfo.entity.RoleTreeEntity;
import com.isoftstone.userinfo.entity.SearchUserInfoEntity;
import com.isoftstone.userinfo.entity.UserInfoTreeEntity;
import com.isoftstone.userinfo.service.UserInfoServicee;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/userinfo")
public class UserInfoController extends BaseController {
    Logger logger =  Logger.getLogger(UserInfoController.class.getName());

    @Resource
    private UserInfoServicee userInfoService2;
    @RequiresPermissions("cldk_data_user")
    @RequestMapping("/page")
    public String  init(){
        return "userinfo/userList";
    }

    //queryUserInfoZtreeList
    @RequiresPermissions("cldk_data_user")
    @RequestMapping("/queryUserInfoZtreeList")
    @ResponseBody
    public List<UserInfoTreeEntity> queryUserInfoZtreeList(@RequestParam("userUuid") String id){
        logger.info(id);
        List<UserInfoTreeEntity> list = userInfoService2.queryUserInfoZtreeList(id);
        return list;
    }

    @RequiresPermissions("cldk_data_user")
    @RequestMapping("/getUserInfolist")
    @ResponseBody
    public PadingRstType<PtUserEntity> getUserInfolist(SearchUserInfoEntity search, PagingBean pagingBean){
//        //request.getParameter("mobile");
       logger.info(search);
        PadingRstType<PtUserEntity> padingRstType = userInfoService2.getUserInfolist(search,pagingBean);
        return padingRstType;
    }

    //getRoleList
    @RequiresPermissions("cldk_data_user")
    @RequestMapping(value = "/updateUserDail")
    @ResponseBody
    //才能返回对应的json字符串
    public String updateUserDail(PtUserEntity ptUserEntity) {
        logger.info(ptUserEntity);
        userInfoService2.updateUserDail(ptUserEntity);
        return buildSuccessJsonMsg("user.update.sucess", null);
    }
    @RequiresPermissions("cldk_data_user")
    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(PtUserEntity ptUserEntity) {
        logger.info(ptUserEntity);
        userInfoService2.addUserInfo(ptUserEntity);
        return buildSuccessJsonMsg("user.add.success", null);
    }
    @RequiresPermissions("cldk_data_user")
    @RequestMapping(value= "/deleteUserInfo")
    @ResponseBody
    public String deleteUserInfo(HttpServletRequest request){
        String ids = request.getParameter("ids");
        logger.info(ids);
        userInfoService2.deleteUserInfo(ids);
        return buildSuccessJsonMsg("user.del.sucess", null);
    }

    //queryRoleInfoZtreeList
    @RequiresPermissions("cldk_data_user")
    @RequestMapping("/queryRoleInfoZtreeList")
    @ResponseBody
    public List<RoleTreeEntity> queryRoleInfoZtreeList(@RequestParam("userUuid") String id){
        logger.info(id);
        List<RoleTreeEntity> list = userInfoService2.queryRoleInfoZtreeList(id);
        return list;
    }
    @RequiresPermissions("cldk_data_user")
    @RequestMapping("/queryUserInfoById")
    @ResponseBody
    public List<PtUserEntity> queryUserInfoById(@RequestParam("userUuid") String id){
        // request.getParameter("parentId");
        logger.info(id);
        List<PtUserEntity> list = userInfoService2.queryUserInfoById(id);
        return list;
    }
    @RequiresPermissions("cldk_data_user")
    @RequestMapping("/bondRoleUser")
    @ResponseBody
    public String bondRoleUser(@RequestParam("userUuid") String userUuid,@RequestParam("roleUuid") String[] roleUuid){
        logger.info(userUuid);
        logger.info(roleUuid);
        userInfoService2.cancelRoleBound(userUuid);
        userInfoService2.bondRoleUser(roleUuid,userUuid);
        return buildSuccessJsonMsg("user.role.allocate.success",null);
    }

}
